package com.ailk.kbi.kbirazor.control
{
	import com.ailk.kbi.kbirazor.common.Util;
	import com.ailk.kbi.kbirazor.main.model.MainModel;
	import com.ailk.kbi.kbirazor.model.KbrModel;
	import com.ailk.kbi.kbirazor.vo.KpiVO;
	import com.ailk.kbi.kbirazor.vo.ResultVO;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import spark.formatters.NumberFormatter;

	public class KbrControl
	{
		internal var _service:RemoteObject;       		
		internal var kbrModel:KbrModel = KbrModel.getInstance();
		
		[Bindable]
		private var mainModel:MainModel = MainModel.getInstance();
		
		public function KbrControl()
		{
			_service = new RemoteObject();            	
			_service.destination = "flexService";
			_service.endpoint = "messagebroker/amf";
			_service.showBusyCursor = true;
			_service.addEventListener(FaultEvent.FAULT, Util.faultHandler);
			_service.getMindMapList.addEventListener(ResultEvent.RESULT, getMindMapListHandler);
			_service.loadMindMapXML.addEventListener(ResultEvent.RESULT, loadMindMapXMLHandler);
			_service.getTimeRangeVO.addEventListener(ResultEvent.RESULT, getTimeRangeVOHandler);
			_service.getKpiValues.addEventListener(ResultEvent.RESULT, getKpiValuesHandler);
			_service.getThresholdSetXML.addEventListener(ResultEvent.RESULT, getThresholdSetXMLHandler);
			
			_service.saveThresholdValue.addEventListener(ResultEvent.RESULT, saveThresholdValueHandler);
			_service.getWarnKpiIds.addEventListener(ResultEvent.RESULT, getWarnKpiIdsHandler);
			_service.getDescDataXML.addEventListener(ResultEvent.RESULT, getDescDataXMLHandler);
			
		}
		
		/**
		 * 获取指标解释数据
		 */
		public function getDescDataXML(condition:String):void{
			_service.getDescDataXML(condition);
		}
		
		private function getDescDataXMLHandler(event:ResultEvent):void{
			var result:ResultVO =  _service.getDescDataXML.lastResult;
			kbrModel.descDataXML = new XML(event.result.xmlString);
		}
		
		/**
		 * 获取报警的kpiIds
		 */
		public function getWarnKpiIds():void{
			_service.getWarnKpiIds(kbrModel.currentDate, mainModel.userVO, kbrModel.kpiIds);
		}
		
		private function getWarnKpiIdsHandler(event:ResultEvent):void{
			kbrModel.warnKpiIds = _service.getWarnKpiIds.lastResult;
		}
		
		
		/**
		 * 保存用户设置后的阈值
		 */
		public function saveThresholdValue():void {
			_service.saveThresholdValue(kbrModel.thresholdSetXML.toString(),mainModel.userVO);
		}
		
		private function saveThresholdValueHandler(event:ResultEvent):void{
			
		}
		
		/**
		 * 获取阈值设置kpiName列表
		 */
		public function getThresholdSetXML():void{
			_service.getThresholdSetXML(kbrModel.kpiIds,mainModel.userVO);
		}
		
		private function getThresholdSetXMLHandler(event:ResultEvent):void{
			var result:ResultVO =  _service.getThresholdSetXML.lastResult;
			kbrModel.thresholdSetXML = new XML(event.result.xmlString);
		}
		
		
		/**
		 * 获取脑图列表
		 */
		public function getMindMapList():void{
			_service.getMindMapList();
		}
		
		private function getMindMapListHandler(event:ResultEvent):void{
			kbrModel.mindMapList = _service.getMindMapList.lastResult;
		}
		
		/**
		 * 获取脑图列表
		 */
		public function loadMindMapXML():void{
			_service.loadMindMapXML(kbrModel.mindMapVO);
		}
		
		private function loadMindMapXMLHandler(event:ResultEvent):void{
			kbrModel.mindMapXML = new XML(_service.loadMindMapXML.lastResult);
		}
		
		public function getTimeRangeVO():void{
			_service.getTimeRangeVO(kbrModel.timeSQL);
		}
		
		private function getTimeRangeVOHandler(event:ResultEvent):void{
			kbrModel.timeRangeVO = _service.getTimeRangeVO.lastResult;
		
		}
		
		public function getKpiValues():void{
			_service.getKpiValues(kbrModel.currentDate,mainModel.userVO, kbrModel.kpiIds);
		}
		
		private function getKpiValuesHandler(event:ResultEvent):void{
			kbrModel.kpiValues = _service.getKpiValues.lastResult;			
		}
		
	}
}